本文主要学习总结下Route,Middleware,Controller间参数传递。开发时经常碰到类似场景:有时需要在Middleware中读取Route中设置的middleware parameter和route parameter,有时也需要在Controller@Action中读取Middleware中设置的参数。
先假设路由是:
Route::group(['prefix' => 'route', 'namespace' => 'RouteParameter'], function () {
// route/user/{user}/role/{role}, route parameter是{user}, {role}
Route::group(['middleware' => 'route.parameter'], function () {
Route::resource('user.role', 'RouteParameterToMiddlewareController');
});
// route/advisor, middleware parameter是 'advisor', 'show', 'comment'
Route::group(['middleware' => 'middleware.parameter:advisor,show,comment'], function () {
Route::resource('advisor', 'MiddlewareParameterToMiddlewareController');
});
// /route/controller, middleware parameter是 'client'
Route::group(['middleware' => 'middleware.controller:client'], function () {
Route::resource('controller', 'MiddlewareToController');
});
});
路由对应的控制器是:
namespace App\Http\Controllers\RouteParameter;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class RouteParameterToMiddlewareController extends Controller
{
public function show($user, $role)
{
}
}
namespace App\Http\Controllers\RouteParameter;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class MiddlewareParameterToMiddlewareController extends Controller
{
public function index()
{
return 'All';
}
}
并且在Kernel.php中注册中间件:
$routeMiddlewares = []
...
'route.parameter' => \App\Http\Middleware\RouteParameter::class,
'middleware.parameter' => \App\Http\Middleware\MiddlewareParameter::class,
'middleware.controller' => \App\Http\Middleware\ControllerParameter::class,
;
Passing Route Parameters to Middleware
在中间件route.parameter
中读取路由参数有三种方式:$request->route($parameter_name)
;$request->route()->parameter($parameter_name)
;$request->route()->parameters()
,代码如下:
<?php
namespace App\Http\Middleware;
use Closure;
class RouteParameter
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// or $user = $request->route('user');
$user = $request->route()->parameter('user'); // {user}
$role = $request->route()->parameter('role'); // {role}
dd($user, $role);
$parameters = $request->route()->parameters(); // ['user' => {user}, 'role' => {role}]
dd($parameters['user'], $parameters['role']);
return $next($request);
}
}
Passing Middleware Parameters to Middleware
在中间件middleware.parameter
中读取中间件参数只需要在handle()
中映射上对应的参数变量就行,就和在Controller@Action
中获取路由参数一样:
namespace App\Http\Middleware;
use Auth;
use Closure;
use Illuminate\Http\Response;
class MiddlewareParameter
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param $role
* @param $action
* @param $data
*
* @return mixed
*/
public function handle($request, Closure $next, $role, $action, $data)
{
dd($role, $action, $data); // 'advisor', 'show', 'comment'
if (Auth::check() && (Auth::user()->type === $role)) {
return $next($request);
}
return abort(Response::HTTP_UNAUTHORIZED, 'You must log in to access the resource.', [
'Set-Cookie' => 'Laravel=0; path=/; Expires=Thu, 01-Jan-1970 00:00:00 GMT; Secure',
]);
}
}
Passing Parameters in Middleware to Controller
有时需要在中间中把参数传递到控制器中,则可以通过Request
对象作为传输管道,代码如:
namespace App\Http\Middleware;
use Closure;
class ControllerParameter
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $role)
{
$request->attributes->add(compact('role')); // 'client'
return $next($request);
}
}
控制器中使用Request对象获取$role参数:
namespace App\Http\Controllers\RouteParameter;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class MiddlewareToController extends Controller
{
public function index(Request $request)
{
dd($request->get('role'));
}
}
总结:Route,Middleware,Controller之间传递参数这个知识点会经常在设计代码时用到,还有从Route中传递参数到Controller中,这个就更是经常要用到的,也简单。总结下这几个知识点,便于查阅。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。